﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using SanctusSoftware.Music.ChordSheetEditor.Wpf;
using System.Windows.Forms.Integration;
using System.Windows.Forms;
using SanctusSoftware.Music.ChordSheetEditor.Client;

namespace Chord_Sheet_Add_in_for_Word
{
  public partial class ChordSheetRibbon : IChordSheetEditorActionView
  {

    #region Setup

    private void ChordSheetRibbon_Load(object sender, RibbonUIEventArgs e)
    {
    //  InitializeComponent();
      SubscribeToEventHandlers();
      SetupTargetGallery();
      
    }

    private void SetupTargetGallery()
    {
      
      _TargetGallery.Items[0].Tag = ActionTarget.NewDocument;
      _TargetGallery.Items[1].Tag = ActionTarget.ThisDocument;
      _TargetGallery.Items[2].Tag = ActionTarget.CurrentSelection;
      _TargetGallery.Items[3].Tag = ActionTarget.CurrentPage;
      _TargetGallery.SelectedItem = _TargetGallery.Items[1];
      SetupSelectedItem();
    }

    private void SubscribeToEventHandlers()
    {
      
      // Target
      _TargetGallery.Click += _TargetGallery_Click;
      
      // Transpose
      _OriginalKeyDropDown.SelectionChanged += this._OriginalKeyDropDown_SelectionChanged;
      _TransposeToDropDown.SelectionChanged += this._TransposeToDropDown_SelectionChanged;
      _TransposeOptionsButton.Click += this._TransposeOptionsButton_Click;
      _TransposeButton.Click += this._TransposeButton_Click;
      _QuickTransposeUpOneLowButton.Click += this._QuickTransposeUpOneLowButton_Click;
      _QuickTransposeUpOneHighButton.Click += this._QuickTransposeUpOneHighButton_Click;
      _QuickTransposeEnharmonicButton.Click += this._QuickTransposeEnharmonicButton_Click;
      _QuickTransposeDownOneLowButton.Click += this._QuickTransposeDownOneLowButton_Click;
      _QuickTransposeDownOneHighButton.Click += this._QuickTransposeDownOneHighButton_Click;

      // Chords
      _FormatChordsButton.Click += this._FormatChordsButton_Click;
      _RemoveHyperlinksButton.Click += this._RemoveHyperlinkButton_Click;
      _RemoveChordsButton.Click += this._MakeLyricSheetButton_Click;

      // About
      _AboutButton.Click += this._AboutButton_Click;

    }
    
    #endregion

    #region IChordSheetEditorActionView Members

    #region Misc

    private bool _IsDevMode = false;
    public bool IsDevMode
    {
      get { return _IsDevMode; }
      set
      {
        _IsDevMode = value;
        _ChordSheetTab.Label = "Chord Sheet";
        if (_IsDevMode)
        {
          _ChordSheetTab.Label += " (Dev)";
        }
      }
    }


    #endregion

    #region Target

    public event EventHandler TargetChanged;

    protected void OnTargetChanged()
    {
      if (TargetChanged != null)
      {
        TargetChanged(this, new EventArgs());
      }
    }

    public ActionTarget Target
    {
      get
      {
        if (_TargetGallery.Tag != null)
        {
          return (ActionTarget)_TargetGallery.Tag;
        }
        else
        {
          throw new Exception("No target defined");
        }
      }
      set
      {
        switch (value)
        {
          case ActionTarget.NewDocument:
            _TargetGallery.SelectedItem = _TargetGallery.Items[0];
            SetupSelectedItem();
            break;
          case ActionTarget.ThisDocument:
            _TargetGallery.SelectedItem = _TargetGallery.Items[1];
            SetupSelectedItem();
            break;
          case ActionTarget.CurrentSelection:
            _TargetGallery.SelectedItem = _TargetGallery.Items[2];
            SetupSelectedItem();
            break;
          case ActionTarget.CurrentPage:
            _TargetGallery.SelectedItem = _TargetGallery.Items[3];
            SetupSelectedItem();
            break;
          default:
            break;
        }
      }
    }

    #endregion

    #region Transpose

    public string[] ValidKeys
    {
      set
      {

        // TODO: Maybe save this somewhere

        foreach (string key in value)
        {
          RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
          item.Label = key;
          item.Tag = key;
          Globals.Ribbons.ChordSheetRibbon._OriginalKeyDropDown.Items.Add(item);
        }

        foreach (string key in value)
        {
          RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
          item.Label = key;
          item.Tag = key;
          Globals.Ribbons.ChordSheetRibbon._TransposeToDropDown.Items.Add(item);
        }
      }
    }

    public event EventHandler RequestTranspose;

    protected void OnRequestTranspose()
    {
      if (RequestTranspose != null)
      {
        RequestTranspose(this, new EventArgs());
      }
    }

    public event EventHandler RequestTransposeOptions;

    protected void OnRequestTransposeOptions()
    {
      if (RequestTransposeOptions != null)
      {
        RequestTransposeOptions(this, new EventArgs());
      }
    }


    public event EventHandler OriginalKeyChanged;

    protected void OnOriginalKeyChanged()
    {
      if (OriginalKeyChanged != null)
      {
        OriginalKeyChanged(this, new EventArgs());
      }
    }

    public string OriginalKey
    {
      get
      {
        if (this._OriginalKeyDropDown.SelectedItem == null)
        {
          return null;
        }
        else
        {
          return (string)this._OriginalKeyDropDown.SelectedItem.Tag;
        }
      }
      set
      {
        foreach (RibbonDropDownItem item in this._OriginalKeyDropDown.Items)
        {
          if (item.Tag.ToString() == value)
          {
            this._OriginalKeyDropDown.SelectedItem = item;
            break;
          }
        }
      }
    }

    public event EventHandler TransposeToChanged;

    protected void OnTransposeToChanged()
    {
      if (TransposeToChanged != null)
      {
        TransposeToChanged(this, new EventArgs());
      }
    }

    public string TransposeTo
    {
      get
      {
        if (this._TransposeToDropDown.SelectedItem == null)
        {
          return null;
        }
        else
        {
          return (string)this._TransposeToDropDown.SelectedItem.Tag;
        }
      }
      set
      {
        foreach (RibbonDropDownItem item in this._TransposeToDropDown.Items)
        {
          if (item.Tag.ToString() == value)
          {
            this._TransposeToDropDown.SelectedItem = item;
            break;
          }
        }
      }
    }

    #endregion

    #region Quick Transpose

    public bool IsQuickTransposeDownLowEnabled
    {
      get
      {
        return _QuickTransposeDownOneLowButton.Enabled;
      }
      set
      {
        _QuickTransposeDownOneLowButton.Enabled = value;
      }
    }

    public string QuickTransposeDownLowKey
    {
      get
      {
        if (_QuickTransposeDownOneLowButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)_QuickTransposeDownOneLowButton.Tag;
        }
      }
      set
      {
        _QuickTransposeDownOneLowButton.Tag = value;
        _QuickTransposeDownOneLowButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeDownLow;

    protected void OnRequestQuickTransposeDownLow()
    {
      if (RequestQuickTransposeDownLow != null)
      {
        RequestQuickTransposeDownLow(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeDownHighEnabled
    {
      get
      {
        return _QuickTransposeDownOneHighButton.Enabled;
      }
      set
      {
        _QuickTransposeDownOneHighButton.Enabled = value;
      }
    }

    public string QuickTransposeDownHighKey
    {
      get
      {
        if (_QuickTransposeDownOneHighButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)_QuickTransposeDownOneHighButton.Tag;
        }
      }
      set
      {
        _QuickTransposeDownOneHighButton.Tag = value;
        _QuickTransposeDownOneHighButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeDownHigh;

    protected void OnRequestQuickTransposeDownHigh()
    {
      if (RequestQuickTransposeDownHigh != null)
      {
        RequestQuickTransposeDownHigh(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeEnharmonicEnabled
    {
      get
      {
        return _QuickTransposeEnharmonicButton.Enabled;
      }
      set
      {
        _QuickTransposeEnharmonicButton.Enabled = value;
      }
    }

    public string QuickTransposeEnharmonicKey
    {
      get
      {
        if (_QuickTransposeEnharmonicButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)_QuickTransposeEnharmonicButton.Tag;
        }
      }
      set
      {
        _QuickTransposeEnharmonicButton.Tag = value;
        _QuickTransposeEnharmonicButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeEnharmonic;

    protected void OnRequestQuickTransposeEnharmonic()
    {
      if (RequestQuickTransposeEnharmonic != null)
      {
        RequestQuickTransposeEnharmonic(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeUpLowEnabled
    {
      get
      {
        return _QuickTransposeUpOneLowButton.Enabled;
      }
      set
      {
        _QuickTransposeUpOneLowButton.Enabled = value;
      }
    }

    public string QuickTransposeUpLowKey
    {
      get
      {
        if (_QuickTransposeUpOneLowButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)_QuickTransposeUpOneLowButton.Tag;
        }
      }
      set
      {
        _QuickTransposeUpOneLowButton.Tag = value;
        _QuickTransposeUpOneLowButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeUpLow;

    protected void OnRequestQuickTransposeUpLow()
    {
      if (RequestQuickTransposeUpLow != null)
      {
        RequestQuickTransposeUpLow(this, new EventArgs());
      }
    }

    public bool IsQuickTransposeUpHighEnabled
    {
      get
      {
        return _QuickTransposeUpOneHighButton.Enabled;
      }
      set
      {
        _QuickTransposeUpOneHighButton.Enabled = value;
      }
    }

    public string QuickTransposeUpHighKey
    {
      get
      {
        if (_QuickTransposeUpOneHighButton.Tag == null)
        {
          return null;
        }
        else
        {
          return (string)_QuickTransposeUpOneHighButton.Tag;
        }
      }
      set
      {
        _QuickTransposeUpOneHighButton.Tag = value;
        _QuickTransposeUpOneHighButton.Label = value;
      }
    }

    public event EventHandler RequestQuickTransposeUpHigh;

    protected void OnRequestQuickTransposeUpHigh()
    {
      if (RequestQuickTransposeUpHigh != null)
      {
        RequestQuickTransposeUpHigh(this, new EventArgs());
      }
    }

    #endregion

    #region Chords

    public event EventHandler RequestFormatChords;

    protected void OnRequestFormatChords()
    {
      if (RequestFormatChords != null)
      {
        RequestFormatChords(this, new EventArgs());
      }
    }

    public event EventHandler RequestRemoveHyperlinksFromChordLines;

    protected void OnRequestRemoveHyperlinksFromChordLines()
    {
      if (RequestRemoveHyperlinksFromChordLines != null)
      {
        RequestRemoveHyperlinksFromChordLines(this, new EventArgs());
      }
    }

    #endregion

    #region Lyrics

    public event EventHandler RequestRemoveChords;

    protected void OnRequestRemoveChords()
    {
      if (RequestRemoveChords != null)
      {
        RequestRemoveChords(this, new EventArgs());
      }
    }

    #endregion

    #endregion

    #region Setup

    
    private void SetupSelectedItem()
    {
      _TargetGallery.Label = _TargetGallery.SelectedItem.Label;
      _TargetGallery.Image = _TargetGallery.SelectedItem.Image;
      _TargetGallery.Tag = _TargetGallery.SelectedItem.Tag;
    }
    


    #endregion

    #region Event Handlers


    #region Target
    
    void _TargetGallery_Click(object sender, RibbonControlEventArgs e)
    {
      SetupSelectedItem();
      OnTargetChanged();
    }


    #endregion

    #region Lyrics

    private void _MakeLyricSheetButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestRemoveChords();
    }

    #endregion

    #region Transpose

    private void _OriginalKeyDropDown_SelectionChanged(object sender, RibbonControlEventArgs e)
    {
      OnOriginalKeyChanged();
    }

    private void _TransposeToDropDown_SelectionChanged(object sender, RibbonControlEventArgs e)
    {
      OnTransposeToChanged();
    }

    private void _TransposeOptionsButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestTransposeOptions();
    }

    private void _TransposeButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestTranspose();
    }

    #endregion

    #region Chords

    private void _FormatChordsButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestFormatChords();
    }

    private void _FormatChordsGalleryButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestFormatChords();
    }

    private void _RemoveHyperlinkButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestRemoveHyperlinksFromChordLines();
    }

    #endregion

    #region Quick Transpose

    private void _QuickTransposeDownOneLowButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeDownLow();
    }

    private void _QuickTransposeDownOneHighButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeDownHigh();
    }

    private void _QuickTransposeEnharmonicButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeEnharmonic();
    }

    private void _QuickTransposeUpOneLowButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeUpLow();
    }

    private void _QuickTransposeUpOneHighButton_Click(object sender, RibbonControlEventArgs e)
    {
      OnRequestQuickTransposeUpHigh();
    }

    #endregion

    #region Formatting

    #endregion

    #endregion

    #region About

    private void _AboutButton_Click(object sender, RibbonControlEventArgs e)
    {
      try
      {
        //new AboutForm().ShowDialog();
        AboutWindow window = new AboutWindow();
        ElementHost.EnableModelessKeyboardInterop(window);
        window.ShowDialog();
      }
      catch (Exception ex)
      {
        MessageBox.Show("Error: " + ex.Message);
        //ExceptionLogger.Log(ex);
      }

    }

    #endregion




  }
}
